package com.chixing.day17.AOP_Proxy.CGLIB_Proxy;

import com.chixing.day16.AOP_Proxy.LogUtil;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 1.生成代理对象
 * 2.实现增强
 */
public class OrderProxy implements MethodInterceptor { //方法拦截器
     private Object target;
    //生成代理对象
    public Object getProxy(Object target) {
        this.target = target;
        Enhancer entry = new Enhancer();
        entry.setSuperclass(target.getClass());
        entry.setCallback(this);
        return entry.create(); // entry 代理对象 (父类为目标对象)
    }



    //实现增强
    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object result = method.invoke(target,args);
        if(method.getName().equals("save")||method.getName().equals("update") || method.getName().equals("delete")){
            new LogUtil().addLog();
        }
        return result;
    } //方法拦截器


}
